package com.lanou.sfu.songforu.mv;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.constant.Url;

/**
 * Created by dllo on 15/10/21.
 */
public class MvActivityFragment extends Fragment {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_mv_tablayout,null);
        mTabLayout = (TabLayout) view.findViewById(R.id.mv_tablayout);
        mViewPager = (ViewPager) view.findViewById(R.id.mv_viewpager);
        return view;


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();

    }


    // 初始化数据
    private void init(){

        //初始化适配器
        MyAdapter adapter = new MyAdapter(getChildFragmentManager());

        // 绑定适配器
        mViewPager.setAdapter(adapter);

        // 页面Tab设置
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabsFromPagerAdapter(adapter);
        mTabLayout.setTabTextColors(0xffbbbbbb, 0xff000000);
        mTabLayout.setSelectedTabIndicatorColor(0xffFF4081);

    }

    public class MyAdapter extends FragmentPagerAdapter {
        private String[] titles = {getActivity().getString(R.string.MV_HOT), getActivity().getString(R.string.MV_NEW)};

//        private String topUrl = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.mv.searchMV&format=json&";
//        private String topUrl2 = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.mv.searchMV&format=json&";
        private String topUrl = Url.MV_TOPURL;
        private String topUrl2 = Url.MV_TOPURL2;

        private String[] url = {topUrl, topUrl2};

        private String num1 = Url.MV_NUM1;
        private String num2 = Url.MV_NUM2;

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return MVFragment.getFragment(position, url[position], num1);

            } else {
                return MVFragment.getFragment(position, url[position], num2);

            }
        }

        @Override
        public int getCount() {
            return titles.length;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

    }
}
